/*
 * ========================================================
 * Copyright(c) 2014 杭州偶尔科技-版权所有
 * ========================================================
 * 本软件由杭州偶尔科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * 
 * 			http://www.kkkd.com/
 * 
 * ========================================================
 */
package com.simon.mengkou.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.ouertech.android.agm.lib.ui.base.defaults.activity.BaseTopActivity;
import com.simon.mengkou.R;
import com.simon.mengkou.alipay.Alipay;
import com.simon.mengkou.alipay.PayResult;
import com.simon.mengkou.data.bean.base.NewOrder;
import com.simon.mengkou.data.bean.base.Order;
import com.simon.mengkou.data.enums.EPayResult;
import com.simon.mengkou.system.constant.CstOuer;
import com.simon.mengkou.system.global.OuerDispatcher;
import com.simon.mengkou.ui.dialog.PayDialog;
import com.simon.mengkou.utils.UtilOuer;
import com.simon.mengkou.wxpay.Wxpay;
import com.tencent.mm.sdk.modelbase.BaseResp;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author : Zhenshui.Xia
 * @since : 2016/4/18.
 * desc    : 支付结果界面
 */
public class PayResultActivity extends BaseTopActivity
        implements Alipay.AlipayListener{

    @Bind(R.id.pay_result_id_img)
    ImageView mIvImg;
    @Bind(R.id.pay_result_id_text)
    ImageView mIvText;
    @Bind(R.id.pay_result_id_btn)
    ImageView mIvBtn;

    //支付订单
    private NewOrder mOrder;
    //支付状态
    private int mStatus;
    private String mGroupBuyId;

    @Override
    protected void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
        Intent intent = getIntent();
        mOrder = (NewOrder) intent.getSerializableExtra(CstOuer.KEY.ORDER);
        mStatus = intent.getIntExtra(CstOuer.KEY.STATUS, EPayResult.SUCCESS);
        mGroupBuyId = intent.getStringExtra(CstOuer.KEY.GROUPID);
    }

    @Override
    protected void initTop() {
        setTitle(R.string.pay_result_title);
        setNavigation(R.drawable.common_ic_left_nav);
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_pay_result);
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);

        if(mStatus == EPayResult.SUCCESS) {
            mIvImg.setImageResource(R.drawable.pay_result_ic_success);
            mIvText.setImageResource(R.drawable.pay_result_ic_success_text);
            mIvBtn.setImageResource(R.drawable.pay_result_ic_success_order);
        } else if(mStatus == EPayResult.FAILURE) {
            mIvImg.setImageResource(R.drawable.pay_result_ic_failure);
            mIvText.setImageResource(R.drawable.pay_result_ic_failure_text);
            mIvBtn.setImageResource(R.drawable.pay_result_ic_failure_repay);
        }

        //注册微信支付结果广播
        registerAction(CstOuer.BROADCAST_ACTION.WEIXIN_PAY_ACTION);
    }

    @Override
    protected void onReceive(Intent intent) {
        super.onReceive(intent);
        //微信支付结果
        if(CstOuer.BROADCAST_ACTION.WEIXIN_PAY_ACTION.equals(intent.getAction())) {
            int code = intent.getIntExtra(CstOuer.KEY.CODE, BaseResp.ErrCode.ERR_SENT_FAILED);
            if(code == BaseResp.ErrCode.ERR_OK) {//支付成功
                OuerDispatcher.presentPayResultActivity(this, mOrder, mGroupBuyId, EPayResult.SUCCESS);
                OuerDispatcher.sendOrderPayBroadcast(this);
                finish();
            } else if(code == BaseResp.ErrCode.ERR_SENT_FAILED) {//微信支付失败
                OuerDispatcher.presentPayResultActivity(this, mOrder, mGroupBuyId, EPayResult.FAILURE);
                finish();
            }
        }
    }


    /**
     *
     */
    @OnClick(R.id.pay_result_id_btn)
    public void goAction() {
        if(mStatus == EPayResult.SUCCESS) {
            OuerDispatcher.presentOrderDetailActivity(this, mOrder.getId(), null);
            finish();
        } else if(mStatus == EPayResult.FAILURE) {
            PayDialog dialog = new PayDialog(this, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int id = v.getId();
                    if (id == R.id.pay_id_alipay) {//支付宝支付
                        new Alipay(PayResultActivity.this).pay(mOrder, PayResultActivity.this);
                    } else if (id == R.id.pay_id_wxpay) {//微信支付
                        new Wxpay(PayResultActivity.this).pay(mOrder);
                    }
                }
            });

            dialog.showAtLocation(mIvBtn, Gravity.CENTER, 0, 0);
        }
    }


    @Override
    public void onResult(PayResult payResult) {
        String resultStatus = payResult.getResultStatus();

        // 判断resultStatus 为“9000”则代表支付成功，具体状态码代表含义可参考接口文档
        if (TextUtils.equals(resultStatus, "9000")) {
            OuerDispatcher.presentPayResultActivity(this, mOrder, mGroupBuyId, EPayResult.SUCCESS);
            OuerDispatcher.sendOrderPayBroadcast(this);
            finish();
        } else {
            OuerDispatcher.presentPayResultActivity(this, mOrder, mGroupBuyId, EPayResult.FAILURE);
            finish();
        }
    }

}
